package com.etouch.security.util;


import com.etouch.security.exception.ProjectException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.http.HttpStatus;

import java.io.Serializable;
import java.util.List;

@Data
@ApiModel(value = "返回类")
public class ResultUtils<T extends Object> implements Serializable {
    @ApiModelProperty(value = "code")
    private Integer code;
    @ApiModelProperty(value = "描述")
    private String msg;
    @ApiModelProperty(value = "对象")
    private T data;
    @ApiModelProperty(value = "数量")
    private Integer size;
    @ApiModelProperty(value = "总计")
    private Integer total;

    /**
     * 默认的失败
     * @return
     */
    public static ResultUtils error(){
        return new ResultUtils(500,"操作失败");
    }
    public static ResultUtils error(String msg){
        return new ResultUtils(500,msg);
    }
    public static ResultUtils error(Integer status,String msg){
        return new ResultUtils(status,msg);
    }
    public static ResultUtils errorParam(String msg){
        return new ResultUtils(400,msg);
    }

    /**
     * 自定义捕获异常返回状态和信息
     * @param e
     * @return
     */
    public static ResultUtils errorSelf(ProjectException e){
        return new ResultUtils(e.getStatus(),e.getMessage());
    }
    /**
     * 判断是否失败
     * @param resultUtils
     * @return
     */
    public static boolean isSuccess(ResultUtils resultUtils){
        return resultUtils.getCode()==200?true:false;
    }
    /**
     * 未授权
     * @param msg
     * @return
     */
    public static ResultUtils errorAuth(String msg){
        return new ResultUtils(405,msg);
    }

    /**
     * 成功 + 返回的成功信息
     * @param data
     * @return
     */
    public static ResultUtils success(Object data){
        return new ResultUtils(200,"操作成功",data);
    }
    public static ResultUtils success(){
        return new ResultUtils(200,"操作成功");
    }
    public static ResultUtils success(String msg,Object data){
        return new ResultUtils(200,msg,data);
    }
    public static ResultUtils ok(String msg){
        return new ResultUtils(200,msg);
    }
    public static  <T> ResultUtils successPage(List<T> list, int total){
        ResultUtils r = new ResultUtils();
        r.setData(list);
        r.setCode(HttpStatus.OK.value());
        if (list.size() > 0){
            r.setMsg("操作成功");
        }else {
            r.setMsg("暂无数据");
        }
        r.setSize(list.size());
        r.setTotal(total);
        return r;
    }

    public ResultUtils(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResultUtils(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResultUtils() {
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}
